package com.flame.system.tools.config;

import com.flame.system.tools.controller.ServerSshController;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import javax.annotation.PostConstruct;
import java.util.Properties;

/**
 * @author wuxintong
 * @since 2022/10/26
 */
@EnableWebSocket
@MapperScan("com.flame.system.tools.mapper")
@ComponentScan("com.flame.system.tools")
@Configuration
public class SystemToolsConfiguration implements WebSocketConfigurer {

    @Autowired
    private ServerSshController serverSshController;

    @PostConstruct
    public void init() {
        Properties properties = new Properties();
        properties.put("resource.loader.file.class", ClasspathResourceLoader.class.getName());
        Velocity.init(properties);
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(serverSshController, "/server").setAllowedOrigins("*");
    }
}
